package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.entity.Column;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.generator.dao.KucunDao;
import io.renren.modules.generator.entity.KucunEntity;
import io.renren.modules.generator.service.ItemService;
import io.renren.modules.generator.service.KucunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("kucunService")
public class KucunServiceImpl extends ServiceImpl<KucunDao, KucunEntity> implements KucunService {

    public static String inc = "zhunairui";
    public static String dec = "ruinaizhu";


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<KucunEntity> page = this.selectPage(
                new Query<KucunEntity>(params).getPage(),
                new EntityWrapper<KucunEntity>().like(params.containsKey("batchNo"), "batch_no", params.get("batchNo") + "")
        );

        return new PageUtils(page);
    }

    @Autowired
    ItemService itemService;

    @Override
    public int decreaceVirKucun(List<KucunEntity> req) {
        if (req == null || req.size() == 0) {
            return 0;
        }
        Condition condi = Condition.create();

        req.stream().forEach(x -> {
            condi.eq("item_guid", x.getItemGuid());
            condi.eq("batch_no", x.getBatchNo());
            condi.orNew();
        });
        condi.eq("1", "2");
        synchronized (dec) {
            List<KucunEntity> kuncuns = this.baseMapper.selectList(condi);
            Map<String, Long> kcMap = kuncuns.stream().collect(Collectors.toMap(KucunEntity::getKey, KucunEntity::getId));
            req.stream().forEach(x -> {
                Long id = kcMap.get(x.getKey());
                if (id != null) {
                    x.setId(id);
                    int result = this.baseMapper.decreaceVirKuncun(x);
                    if (result == 0) {
                        throw new RuntimeException("库存不足");
                    }
                } else {//没有库存记录，新创建一条
                    throw new RuntimeException("库存不足");

                }
            });
            return 1;
        }
    }

    @Override
    public int decreaceKucun(List<KucunEntity> req) {
        if (req == null || req.size() == 0) {
            return 0;
        }
        Condition condi = Condition.create();

        req.stream().forEach(x -> {
            condi.eq("item_guid", x.getItemGuid());
            condi.eq("batch_no", x.getBatchNo());
            condi.orNew();
        });
        condi.eq("1", "2");
        synchronized (dec) {
            List<KucunEntity> kuncuns = this.baseMapper.selectList(condi);
            Map<String, Long> kcMap = kuncuns.stream().collect(Collectors.toMap(KucunEntity::getKey, KucunEntity::getId));
            req.stream().forEach(x -> {
                Long id = kcMap.get(x.getKey());
                if (id != null) {
                    x.setId(id);
                    int result = this.baseMapper.decreaceKuncun(x);
                    if (result == 0) {
                        throw new RuntimeException("库存不足");
                    }
                } else {//没有库存记录，新创建一条
                    throw new RuntimeException("库存不足");

                }
            });
            return 1;
        }

    }

    @Override
    public int increaceKucun(List<KucunEntity> req) {
        if (req == null || req.size() == 0) {
            return 0;
        }
        Condition condi = Condition.create();

        req.stream().forEach(x -> {
            condi.eq("item_guid", x.getItemGuid());
            condi.eq("batch_no", x.getBatchNo());
            condi.orNew();
        });
        condi.eq("1", "2");
        synchronized (inc) {
            List<KucunEntity> kuncuns = this.baseMapper.selectList(condi);
            Map<String, Long> kcMap = kuncuns.stream().collect(Collectors.toMap(KucunEntity::getKey, KucunEntity::getId));
            req.stream().forEach(x -> {
                Long id = kcMap.get(x.getKey());
                if (id != null) {
                    x.setId(id);
                    this.baseMapper.increaceKuncun(x);
                } else {//没有库存记录，新创建一条
                    KucunEntity insert = new KucunEntity();
                    insert.setItemGuid(x.getItemGuid());
                    insert.setBatchNo(x.getBatchNo());
                    insert.setVirAmount(x.getVirAmount());
                    insert.setAmount(x.getAmount());
                    insert.setCreateTime(new Date());
                    insert.setCreateUser("入库创建");
                    insert.setSpecId(itemService.selectOne(Condition.create().eq("guid", x.getItemGuid()).setSqlSelect(Column.create().column("spec_id"))).getSpecId());
                    this.baseMapper.insert(insert);//todo 没有考虑多线程插入的情况
                }
            });
            return 1;
        }

    }
}
